<script setup lang="ts">
import {
  adviceDetail,
  AdviceDetail,
  AdviceForm,
  adviceList,
  AdviceQuery,
  deleteAdvice,
  updateAdvice
} from "@/api/advice";
import {statusOpts_2} from "@/utils/status";
import defaultSettings from "@/settings";


defineOptions({
  name: "Advice",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);
const objFormRef = ref(ElForm);

const statusOptions = statusOpts_2();

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<AdviceQuery>({
  pageNum: 1,
  pageSize: 10,
});

const lists = ref<AdviceDetail[]>();

const dialog = reactive({
  title: "",
  visible: false,
});

const formData = reactive<AdviceForm>({
  reply: "",
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  adviceList(queryParams)
    .then(({data}) => {
      let list = data.items || null;
      if (list) {
        for (const k in list) {
          if (list[k].pictures.length > 0) {
            for (const j in list[k].pictures) {
              list[k].pictures[j] = defaultSettings.imgDomain + list[k].pictures[j];
            }
          }
        }
      }

      lists.value = list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 角色保存提交 */
function handleSubmit() {
  objFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const roleId = formData.id;
      if (roleId) {
        updateAdvice(roleId, formData)
          .then(() => {
            ElMessage.success("处理成功");
            handleQuery();
            closeDialog();
            // resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  objFormRef.value.resetFields();
  objFormRef.value.clearValidate();

  formData.id = undefined;
}

/** 打开角色表单弹窗 */
function openDialog(roleId?: number) {
  dialog.visible = true;
  if (roleId) {
    dialog.title = "处理反馈";
    adviceDetail(roleId)
      .then(({data}) => {
        Object.assign(formData, data);
      });
  }
}

/** 删除 */
function handleDelete(roleId: number) {
  if (!roleId) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    deleteAdvice(roleId)
      .then(() => {
        ElMessage.success("删除成功");
        // resetQuery();
        handleQuery();
      })
      .finally(() => (loading.value = false));
  });
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <!--    <div class="search-container">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item prop="keywords" label="">
              <el-input v-model="queryParams.keywords"  placeholder="角色名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <i-ep-search/>
                搜索
              </el-button>
              <el-button @click="resetQuery">
                <i-ep-refresh/>
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>-->

    <el-card shadow="never" class="table-container">
      <el-table v-loading="loading" :data="lists">
        <el-table-column label="编号" align="center" prop="id" width="60"/>
        <el-table-column label="订单号" align="center" prop="object_id">
          <template #default="scope"> {{ scope.row.object_id || "" }}</template>
        </el-table-column>
        <el-table-column label="反馈人" align="center" prop="create_username">
          <template #default="scope">
            {{ scope.row.create_user ? (scope.row.create_user.username || scope.row.create_user.id) : '' }}
          </template>
        </el-table-column>
        <el-table-column label="联系电话" align="center" prop="contact_phone" width="60"/>
        <el-table-column label="反馈类型" align="center" prop="type">
          <template #default="scope">{{ scope.row.type == 1 ? '投诉' : '建议' }}</template>
        </el-table-column>
        <el-table-column label="反馈内容" align="center" prop="content"/>
        <el-table-column label="反馈图片" align="center" prop="pictures">
          <template #default="scope">
            <div class="demo-image__preview">
              <el-image
                :src="path"
                v-for="(path, idx) in scope.row.pictures"
                :preview-src-list="scope.row.pictures"
                :initial-index="idx"
                :zoom-rate="1.2"
                :min-scale="0.2"
                :max-scale="2"
                :z-index="999"
                fit="cover"
                style="margin-bottom: 5px;width: 60px;height: 60px;"/>
            </div>

          </template>
        </el-table-column>
        <el-table-column label="反馈时间" align="center" prop="create_time"/>
        <el-table-column label="处理时间" align="center" prop="handle_time"/>
        <el-table-column label="备注" align="center" prop="reply"/>
        <el-table-column label="状态" align="center" width="160">
          <template #default="scope">
            <el-tag :type="scope.row.status == 1 ? 'success' : 'danger'">
              {{ statusOptions[scope.row.status] }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="80">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="openDialog(scope.row.id)" v-if="scope.row.status == 0">
              处理反馈
            </el-button>
          </template>
        </el-table-column>

      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize" @pagination="handleQuery"/>
    </el-card>

    <!-- 角色表单弹窗 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" @close="closeDialog">
      <el-form ref="objFormRef" :model="formData" label-width="140px">
        <el-form-item label="备注" prop="name">
          <el-input type="textarea" v-model="formData.reply" placeholder="请输入备注"/>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="Number(ok)" v-for="(op, ok) in statusOptions" :key="ok">{{ op }}</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>
<style scoped>
.demo-image__error .image-slot {
  font-size: 30px;
}

.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}

.demo-image__error .el-image {
  width: 100%;
  height: 200px;
}

:deep(.el-table) .el-table__cell {
  z-index: initial !important;
}
/*
:deep(.el-table) tr {
  z-index: 1;
}

:deep(.el-image-viewer__wrapper) {
  z-index: 1;
}

.el-image-viewer__mask {
  z-index: 1;
}*/
</style>
